/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package facade;

import entity.Studente;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

/**
 *
 * @author grillo
 */
@Stateless
public class StudenteFacade implements StudenteFacadeLocal, StudenteFacadeRemote {
    @PersistenceContext
    private EntityManager em;

    public void create(Studente studente) {
        em.persist(studente);
    }

    public void edit(Studente studente) {
        em.merge(studente);
    }

    public void remove(Studente studente) {
        em.remove(em.merge(studente));
    }

    public Studente find(Object id) {
        return em.find(Studente.class, id);
    }

    public List<Studente> findAll() {
        return em.createQuery("select object(o) from Studente as o").getResultList();
    }

    public Studente findByMatricola(Integer matricola) {
        try {
            Studente d = (Studente)(em.createNamedQuery("Studente.findByMatricola").setParameter("matricola", matricola).getSingleResult());
            return d;
        } catch (NoResultException e) {
            return null;
        }
    }

    public Studente login(Integer mat, String pwd) {
        try {
            Studente u = (Studente)(em.createNamedQuery("Studente.login").setParameter("matricola", mat).setParameter("password", pwd).getSingleResult());
            return u;
        } catch (NoResultException e) {
            return null;
        }
    }
}
